<template>
  <el-card>
    <Headers
      :btn-show="true"
      btn-text="添加角色"
      :thickness-is-show="tableThickness"
      @onShowhickness="onShowhickness"
    />
    <el-dialog title="添加用户" :visible.sync="tableThickness" />
    <el-table :data="datass" style="width: 100%" stripe border>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-row
            v-for="tag in props.row.children"
            :key="tag.id"
            class="rowtag"
          >
            <el-col :span="5">
              <el-tag
                closable
                :disable-transitions="false"
                @close="handleClose(props.row.id,tag.id)"
              >
                {{ tag.authName }}
              </el-tag>
              <i class="el-icon-caret-right" />
            </el-col>
            <el-col :span="19" class="coltar">
              <el-row v-for="item in tag.children" :key="item.id">
                <el-col :span="6">
                  <el-tag type="success" closable @close="handleClose(props.row.id,item.id)">
                    {{ item.authName }}
                  </el-tag>
                  <i class="el-icon-caret-right" />
                </el-col>

                <el-col :span="18">
                  <el-tag
                    v-for="tag3 in item.children"
                    :key="tag3.id"
                    type="warning"
                    closable
                    style="margin: 7px;"
                    @close="handleClose(props.row.id,tag3.id)"
                  >
                    {{ tag3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" />
      <el-table-column label="角色名称" prop="roleDesc" />
      <el-table-column label="角色描述" prop="roleName" />
      <el-table-column label="操作" prop="desc" width="300">
        <template v-slot="{ row }">
          <el-row type="flex" justify="center">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="tableModifys(row)"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="open(row.id)"
            >删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="Authoritys(row)"
            >分配权限</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加角色弹层 -->
    <el-dialog title="添加角色" :visible.sync="tableThickness">
      <tablecharacter :table-thickness.sync="tableThickness" @tableRoles="tableRoles" />
    </el-dialog>
    <!-- 修改弹层 -->
    <div class="" />
    <el-dialog v-if="tableModify" title="编辑角色" :visible.sync="tableModify">
      <tablecharacter :table-thickness.sync="tableModify" :row-list="rowList" @tableRoles="tableRoles" />
    </el-dialog>
    <!-- 权限弹层 -->
    <el-dialog
      v-if="tableAuthority"
      title="分配权限"
      :visible.sync="tableAuthority"
    >
      <div>
        <table-authority
          :authority-list="AuthorityList"
          :table-authority.sync="tableAuthority"
          @tableRoles="tableRoles"
        />
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import Headers from '@/components/Headers'
import Tablecharacter from './component/Tablecharacter.vue'
import TableAuthority from './component/tableAuthority.vue'
import { tableRoles, removeTag, removeRow } from '@/api/table'
export default {
  name: 'Table',
  components: { Headers, Tablecharacter, TableAuthority },
  props: {},
  data() {
    return {
      tableThickness: false,
      tableModify: false, // 修改弹层
      tableAuthority: false, // 权限弹层
      datass: [],
      AuthorityList: null,
      rowList: {} // 修改数据
    }
  },
  computed: {}, // 计算属性
  watch: {},
  created() {
    this.tableRoles()
  },
  mounted() {},
  methods: {
    async tableRoles() {
      try {
        const datass = await tableRoles()
        this.datass = datass.data.data
        this.$message.success('数据更新成功！')
      } catch (error) {
        console.log(error)
        this.$message.error('数据更新失败!!')
      }
    },
    onShowhickness(isShow) {
      this.tableThickness = isShow
    },
    open(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await removeRow(id)
          this.tableRoles()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleClose(rowId, tagId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await removeTag(rowId, tagId)
          this.tableRoles()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      // this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    // 权限
    Authoritys(row) {
      this.AuthorityList = row
      this.tableAuthority = true
    },
    // 修改
    tableModifys(row) {
      this.rowList = row
      this.tableModify = true
    }

  }
}
</script>

<style scoped>
.rowtag {
  border: 1px solid #eeeeee;
  padding-left: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.coltar .el-row {
  border-top: 1px solid #eeeeee;
  display: flex;
   justify-content: center;
  align-items: center;
}
.coltar .el-row:nth-child(1) {
  border: 0;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
